import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/game/game_detail/models/model_game_round.dart';
import 'package:flutter_09_game/pages/game/game_detail/widgets/widget_round_ball.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/button/debounce_gesture_detector.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 历史记录表格组件
/// 用于显示往期开奖记录
class HistoryTableWidget extends StatelessWidget {
  final List<ModelGameRoundHistory> historyList;
  final VoidCallback? onMoreTap;

  // final Function(int round)? onVerifyTap;

  const HistoryTableWidget({
    super.key,
    required this.historyList,
    this.onMoreTap,
    // this.onVerifyTap,
  });

  @override
  Widget build(BuildContext context) {
    // 最多显示10条数据
    final displayList = historyList.take(10).toList();

    return Container(
      color: AppColor.navBgColor,
      child: Column(
        children: [
          _buildHeader(),
          _buildTableHeader(),
          4.verticalSpaceFromWidth,
          ...displayList.asMap().entries.map(
              (entry) => _buildTableRow(entry.value, isFirst: entry.key == 0)),
        ],
      ),
    );
  }

  /// 构建顶部标题区域
  Widget _buildHeader() {
    return Container(
      height: 40.w,
      padding: EdgeInsets.symmetric(
        horizontal: 12.w,
      ).copyWith(top: 8.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            LocaleKeys.historyDraw.tr,
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              color: AppColor.textPrimary.withOpacity(0.94),
            ),
          ),
          DebounceGestureDetector(
            onTap: onMoreTap,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  LocaleKeys.more.tr,
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                    color: AppColor.primary,
                  ),
                ),
                // 4.horizontalSpace,
                AutoImage(
                  Assets.gameArrowRight,
                  width: 20.w,
                  height: 20.w,
                  color: AppColor.primary,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 构建表格头部
  Widget _buildTableHeader() {
    return Container(
      height: 40.w,
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      // decoration: BoxDecoration(
      //   color: AppColor.primary.withOpacity(0.3),
      // ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildHeaderCell(
            LocaleKeys.lotteryIssue.tr,
          ),
          _buildHeaderCell(
            LocaleKeys.drawBlock.tr,
          ),
          _buildHeaderCell(
            LocaleKeys.drawResult.tr,
          ),
          _buildHeaderCell(LocaleKeys.blockHash.tr, width: 90),
          _buildHeaderCell(LocaleKeys.action.tr, width: 40),
        ],
      ),
    );
  }

  /// 构建表格头部单元格
  Widget _buildHeaderCell(String title, {double width = 60}) {
    return Container(
      height: 40.w,
      width: width.w,
      alignment: Alignment.centerLeft,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w600,
          color: AppColor.textPrimary.withOpacity(0.3),
        ),
      ),
    );
  }

  /// 构建表格数据行
  Widget _buildTableRow(ModelGameRoundHistory item, {bool isFirst = false}) {
    return Container(
      height: 36.w,
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: isFirst ? AppColor.primary.withOpacity(0.3) : null,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // 开奖期数
          _buildDataCell(
            '${item.issue ?? 0}期',
            // flex: 2,
          ),
          // 开奖区块
          _buildDataCell(
            '${item.round ?? 0}',
            // flex: 2,
          ),
          // 开奖结果
          _buildResultCell(item.result ?? 0),
          // 区块哈希
          _buildHashCell(item.blockHash ?? '', (item.result)),
          // 操作
          _buildActionCell(item.round ?? 0),
        ],
      ),
    );
  }

  /// 构建数据单元格
  Widget _buildDataCell(String text, {double width = 60}) {
    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      width: width.w,
      alignment: Alignment.centerLeft,
      child: Text(
        text,
        style: TextStyle(
          fontSize: 12.sp,
          fontWeight: FontWeight.w400,
          color: AppColor.textPrimary.withOpacity(0.94),
        ),
      ),
    );
  }

  /// 构建结果单元格（包含图标和数字）
  Widget _buildResultCell(int result) {
    return Container(
      width: 60.w,
      alignment: Alignment.centerLeft,
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      // child: Center(
      child: Row(
        mainAxisSize: MainAxisSize.min,

        children: [
          // GameResultIcon(
          //   result: result,
          //   size: 20.w,
          // ),

          WidgetRoundBall(result: result),
          4.horizontalSpace,
          Text(
            result.toString(),
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w600,
              color: result % 2 == 1
                  ? AppColor.homePrizePoolIndicator
                  : AppColor.gameResultEven,
            ),
          ),
        ],
        // ),
        // ),
      ),
    );
  }

  /// 构建哈希单元格
  Widget _buildHashCell(String blockHash, int? result) {

    return SizedBox(
      width: 90.w,
      child:
      UtilsOther.buildHashDigitsViewString(blockHash, result)


    );
  }

  /// 构建操作单元格
  Widget _buildActionCell(int round) {
    return SizedBox(
      width: 40.w,
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      // child: Center(
      child: DebounceGestureDetector(
        onTap: () {
          UtilsOther.openVerify(round);
        },
        child: Text(
          LocaleKeys.verify.tr,
          style: TextStyle(
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
            color: AppColor.primary,
          ),
        ),
      ),
      // ),
      // ),
    );
  }

}
